

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>toydata.Tree &mdash; ToyData 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> ToyData
          

          
          </a>

          
            
            
              <div class="version">
                1.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../Intro.html">Introductions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Overview.html">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules.html">toydata</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">ToyData</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>toydata.Tree</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for toydata.Tree</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">abc</span> <span class="kn">import</span> <span class="n">ABCMeta</span><span class="p">,</span> <span class="n">abstractmethod</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Iterator</span>
<span class="kn">from</span> <span class="nn">toydata.Queue</span> <span class="kn">import</span> <span class="n">LinkedQueue</span>


<div class="viewcode-block" id="Tree"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree">[docs]</a><span class="k">class</span> <span class="nc">Tree</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">ABCMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Abstract base class representing a tree structure.&quot;&quot;&quot;</span>

    <span class="c1"># nested Position class</span>
<div class="viewcode-block" id="Tree.Position"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.Position">[docs]</a>    <span class="k">class</span> <span class="nc">Position</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">ABCMeta</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;An abstraction representation the location of a single element.&quot;&quot;&quot;</span>
<div class="viewcode-block" id="Tree.Position.element"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.Position.element">[docs]</a>        <span class="nd">@abstractmethod</span>
        <span class="k">def</span> <span class="nf">element</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Return the element store at this Position&quot;&quot;&quot;</span></div>

        <span class="nd">@abstractmethod</span>
        <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Return True if other Position represent the same location&quot;&quot;&quot;</span>

        <span class="k">def</span> <span class="fm">__ne__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Return True if other does not represent the same location&quot;&quot;&quot;</span>
            <span class="k">return</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">)</span></div>

    <span class="c1"># Some abstract methods that concrete subclass must support</span>
<div class="viewcode-block" id="Tree.root"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.root">[docs]</a>    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">root</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return Position representing the tree&#39;s root(or None if empty)&quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="Tree.parent"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.parent">[docs]</a>    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">parent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return Position representing p&#39;s parent(or None if p is root)&quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="Tree.num_children"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.num_children">[docs]</a>    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">num_children</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of children that Position p has&quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="Tree.children"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.children">[docs]</a>    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">children</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Iterator</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Generate an iteration of Positions representing p&#39;s children&quot;&quot;&quot;</span></div>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the total number of element in the tree&quot;&quot;&quot;</span>

    <span class="c1"># Some concrete methods implemented in the class</span>
<div class="viewcode-block" id="Tree.is_root"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.is_root">[docs]</a>    <span class="k">def</span> <span class="nf">is_root</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if Position p represent the root of the tree&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">()</span> <span class="o">==</span> <span class="n">p</span></div>

<div class="viewcode-block" id="Tree.is_leaf"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.is_leaf">[docs]</a>    <span class="k">def</span> <span class="nf">is_leaf</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if Position p does not have any children&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_children</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span></div>

<div class="viewcode-block" id="Tree.is_empty"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.is_empty">[docs]</a>    <span class="k">def</span> <span class="nf">is_empty</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if the tree is empty&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__len__</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span></div>

<div class="viewcode-block" id="Tree.depth"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.depth">[docs]</a>    <span class="k">def</span> <span class="nf">depth</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of levels separating Position p from the root</span>

<span class="sd">        Top -&gt; Bottom</span>

<span class="sd">        Time complexity: O(d_p + 1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_root</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">1</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="p">(</span><span class="n">p</span><span class="p">))</span></div>

    <span class="k">def</span> <span class="nf">_height</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the height of the subtree rooted at Position p&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_leaf</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">1</span> <span class="o">+</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_height</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">(</span><span class="n">p</span><span class="p">))</span>

<div class="viewcode-block" id="Tree.height"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.height">[docs]</a>    <span class="k">def</span> <span class="nf">height</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the height of the subtree rooted at Position P.</span>

<span class="sd">        If p is None ,return the height of the entire tree</span>

<span class="sd">        Time complexity: O(n)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">p</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_height</span><span class="p">(</span><span class="n">p</span><span class="p">)</span></div>

    <span class="c1"># Implementing Tree Traversals</span>
<div class="viewcode-block" id="Tree.preorder"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.preorder">[docs]</a>    <span class="k">def</span> <span class="nf">preorder</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate a preorder iteration of positions in the tree.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subtree_preorder</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">()):</span>
                <span class="k">yield</span> <span class="n">p</span></div>

    <span class="k">def</span> <span class="nf">_subtree_preorder</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate a preorder iteration of positions</span>
<span class="sd">        in subtree rooted at p.&quot;&quot;&quot;</span>
        <span class="k">yield</span> <span class="n">p</span>
        <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">other</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subtree_preorder</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
                <span class="k">yield</span> <span class="n">other</span>

<div class="viewcode-block" id="Tree.postorder"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.postorder">[docs]</a>    <span class="k">def</span> <span class="nf">postorder</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate a postorder iteration of positions in the tree.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subtree_postorder</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">()):</span>
                <span class="k">yield</span> <span class="n">p</span></div>

    <span class="k">def</span> <span class="nf">_subtree_postorder</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate a postorder iteration of positions</span>
<span class="sd">        in subtree rooted at p.&quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">other</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subtree_postorder</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
                <span class="k">yield</span> <span class="n">other</span>
        <span class="k">yield</span> <span class="n">p</span>

<div class="viewcode-block" id="Tree.breadthfirst"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.Tree.breadthfirst">[docs]</a>    <span class="k">def</span> <span class="nf">breadthfirst</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate a breath-first iteration of the positions of the tree&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
            <span class="n">fringe</span> <span class="o">=</span> <span class="n">LinkedQueue</span><span class="p">()</span>
            <span class="n">fringe</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">())</span>
            <span class="k">while</span> <span class="ow">not</span> <span class="n">fringe</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
                <span class="n">p</span> <span class="o">=</span> <span class="n">fringe</span><span class="o">.</span><span class="n">dequeue</span><span class="p">()</span>
                <span class="k">yield</span> <span class="n">p</span>
                <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
                    <span class="n">fringe</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="n">c</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="BinaryTree"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.BinaryTree">[docs]</a><span class="k">class</span> <span class="nc">BinaryTree</span><span class="p">(</span><span class="n">Tree</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Abstract base class representing a binary tree structure&quot;&quot;&quot;</span>
    <span class="c1"># additional abstract methods</span>
<div class="viewcode-block" id="BinaryTree.left"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.BinaryTree.left">[docs]</a>    <span class="k">def</span> <span class="nf">left</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a Position representing p&#39;s left child</span>

<span class="sd">        Return None if p does not have a left child</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;must be implemented by subclass&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="BinaryTree.right"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.BinaryTree.right">[docs]</a>    <span class="k">def</span> <span class="nf">right</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a Position representing p&#39;s right child</span>

<span class="sd">        Return None if p does not have a right child</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;must be implemented by subclass&#39;</span><span class="p">)</span></div>

    <span class="c1"># concrete methods implemented in this class</span>
<div class="viewcode-block" id="BinaryTree.sibling"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.BinaryTree.sibling">[docs]</a>    <span class="k">def</span> <span class="nf">sibling</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a Position representing p&#39;s sibling(or None if no sibling)&quot;&quot;&quot;</span>
        <span class="n">parent</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">parent</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>  <span class="c1"># p is the root</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">p</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="n">parent</span><span class="p">):</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">right</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span></div>

<div class="viewcode-block" id="BinaryTree.children"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.BinaryTree.children">[docs]</a>    <span class="k">def</span> <span class="nf">children</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate an iteration of Positions representing p&#39;s children&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">yield</span> <span class="bp">self</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">right</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">yield</span> <span class="bp">self</span><span class="o">.</span><span class="n">right</span><span class="p">(</span><span class="n">p</span><span class="p">)</span></div>

    <span class="c1"># In-order iteration</span>
    <span class="c1"># We can not define inorder traversal in a general multi-wway tree,</span>
    <span class="c1"># so we define it only in binary tree.</span>
<div class="viewcode-block" id="BinaryTree.inorder"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.BinaryTree.inorder">[docs]</a>    <span class="k">def</span> <span class="nf">inorder</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate an inorder iteration of positions in the tree&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subtree_inorder</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">()):</span>
                <span class="k">yield</span> <span class="n">p</span></div>

    <span class="k">def</span> <span class="nf">_subtree_inorder</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate an inorder iteration of positions in the tree&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">other</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subtree_inorder</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="n">p</span><span class="p">)):</span>
                <span class="k">yield</span> <span class="n">other</span>
        <span class="k">yield</span> <span class="n">p</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">right</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">other</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subtree_inorder</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">right</span><span class="p">(</span><span class="n">p</span><span class="p">)):</span>
                <span class="k">yield</span> <span class="n">other</span>

    <span class="c1"># positions and itertation</span>
<div class="viewcode-block" id="BinaryTree.positions"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.BinaryTree.positions">[docs]</a>    <span class="k">def</span> <span class="nf">positions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trav_method</span><span class="o">=</span><span class="s1">&#39;preorder&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate an iteration of the tree&#39;s positions</span>

<span class="sd">        Methods: preorder, postorder, breadthfirst, inorder</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">trav_method</span> <span class="o">==</span> <span class="s1">&#39;preorder&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">preorder</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">trav_method</span> <span class="o">==</span> <span class="s1">&#39;postorder&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">postorder</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">trav_method</span> <span class="o">==</span> <span class="s1">&#39;breathfirst&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">breadthfirst</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">trav_method</span> <span class="o">==</span> <span class="s1">&#39;inorder&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">inorder</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="fm">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate an iteration of the tree&#39;s elements&quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">positions</span><span class="p">():</span>
            <span class="k">yield</span> <span class="n">p</span><span class="o">.</span><span class="n">element</span><span class="p">()</span></div>


<div class="viewcode-block" id="LinkedBinaryTree"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.LinkedBinaryTree">[docs]</a><span class="k">class</span> <span class="nc">LinkedBinaryTree</span><span class="p">(</span><span class="n">BinaryTree</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Linked representation of a binary tree structure&quot;&quot;&quot;</span>

    <span class="k">class</span> <span class="nc">_Node</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Lightweight, nonpublic class for storing a node&quot;&quot;&quot;</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="s1">&#39;_element&#39;</span><span class="p">,</span> <span class="s1">&#39;_parent&#39;</span><span class="p">,</span> <span class="s1">&#39;_left&#39;</span><span class="p">,</span> <span class="s1">&#39;_right&#39;</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">element</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">left</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">right</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_element</span> <span class="o">=</span> <span class="n">element</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span> <span class="o">=</span> <span class="n">parent</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_left</span> <span class="o">=</span> <span class="n">left</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_right</span> <span class="o">=</span> <span class="n">right</span>

<div class="viewcode-block" id="LinkedBinaryTree.Position"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.LinkedBinaryTree.Position">[docs]</a>    <span class="k">class</span> <span class="nc">Position</span><span class="p">(</span><span class="n">BinaryTree</span><span class="o">.</span><span class="n">Position</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;An abstraction representing the location of a single element&quot;&quot;&quot;</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">container</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Constructor should not be invoked by user&quot;&quot;&quot;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_container</span> <span class="o">=</span> <span class="n">container</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_node</span> <span class="o">=</span> <span class="n">node</span>

        <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_node</span><span class="o">.</span><span class="n">_element</span><span class="p">)</span>

<div class="viewcode-block" id="LinkedBinaryTree.Position.element"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.LinkedBinaryTree.Position.element">[docs]</a>        <span class="k">def</span> <span class="nf">element</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Return the element at this Position&quot;&quot;&quot;</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_node</span><span class="o">.</span><span class="n">_element</span></div>

        <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Return True if other is a Position representing</span>
<span class="sd">            the same location&quot;&quot;&quot;</span>
            <span class="c1"># note here we use `is` to compare different nodes</span>
            <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="n">other</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="ow">and</span> <span class="n">other</span><span class="o">.</span><span class="n">_node</span> <span class="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">_node</span></div>

    <span class="k">def</span> <span class="nf">_validate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return associated node, if position is valid&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Position</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;p must be proper Position type&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">_container</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">self</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;p does not belong to this container&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">_node</span><span class="o">.</span><span class="n">_parent</span> <span class="ow">is</span> <span class="n">p</span><span class="o">.</span><span class="n">_node</span><span class="p">:</span>  <span class="c1"># convention for deprecated nodes</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;p is no longer valid&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">p</span><span class="o">.</span><span class="n">_node</span>

    <span class="k">def</span> <span class="nf">_make_position</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return Position instance for given node(or None if no node)&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">Position</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">)</span> <span class="k">if</span> <span class="n">node</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="kc">None</span>

    <span class="c1"># binary tree constrcture</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create an initially empty binary tree&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_root</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_Node</span><span class="p">(</span><span class="n">root</span><span class="p">)</span> <span class="k">if</span> <span class="n">root</span> <span class="k">else</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_size</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">root</span> <span class="k">else</span> <span class="mi">0</span>

    <span class="c1"># public access</span>
    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the total number of elements in the tree</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_size</span>

    <span class="c1"># printing</span>
<div class="viewcode-block" id="LinkedBinaryTree.preorder_indent"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.LinkedBinaryTree.preorder_indent">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">preorder_indent</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Print preorder representation of subtree of T rooted at p</span>
<span class="sd">        at depth d.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">p</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;Empty&#39;</span>
        <span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">def</span> <span class="nf">recur</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
            <span class="k">nonlocal</span> <span class="n">s</span>
            <span class="n">s</span>  <span class="o">+=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">d</span> <span class="o">*</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">element</span><span class="p">())</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>
            <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">T</span><span class="o">.</span><span class="n">children</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
                <span class="n">recur</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">recur</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">s</span></div>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">LinkedBinaryTree</span><span class="o">.</span><span class="n">preorder_indent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">(),</span> <span class="mi">0</span><span class="p">)</span>


<div class="viewcode-block" id="LinkedBinaryTree.root"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.LinkedBinaryTree.root">[docs]</a>    <span class="k">def</span> <span class="nf">root</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the root Position of the tree(or None if tree is empty)</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_position</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">)</span></div>

<div class="viewcode-block" id="LinkedBinaryTree.parent"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.LinkedBinaryTree.parent">[docs]</a>    <span class="k">def</span> <span class="nf">parent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the Position of p&#39;s parent(or None if p is root)</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">node</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_validate</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_position</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">_parent</span><span class="p">)</span></div>

<div class="viewcode-block" id="LinkedBinaryTree.left"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.LinkedBinaryTree.left">[docs]</a>    <span class="k">def</span> <span class="nf">left</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the Position of p&#39;s left child(or None if no left child)</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">node</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_validate</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_position</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">_left</span><span class="p">)</span></div>

<div class="viewcode-block" id="LinkedBinaryTree.right"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.LinkedBinaryTree.right">[docs]</a>    <span class="k">def</span> <span class="nf">right</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the Position of p&#39;s right child(or None if no right child)</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">node</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_validate</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_position</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">_right</span><span class="p">)</span></div>

<div class="viewcode-block" id="LinkedBinaryTree.num_children"><a class="viewcode-back" href="../../toydata.html#toydata.Tree.LinkedBinaryTree.num_children">[docs]</a>    <span class="k">def</span> <span class="nf">num_children</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of children of Position p</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">node</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_validate</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">_left</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">_right</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">count</span></div>

    <span class="c1"># nonpublic mutators</span>
    <span class="k">def</span> <span class="nf">_add_root</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Place element e at the root of an empty tree and</span>
<span class="sd">        return new Position.</span>

<span class="sd">        Raise ValueError if tree nonempty.</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Root exists&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_size</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_root</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_Node</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_position</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_add_left</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">e</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a new left child for Position p, storing element e.</span>

<span class="sd">        Return the Position of new node.</span>
<span class="sd">        Raise ValueError if Position p is invalid or p already has a</span>
<span class="sd">        left child.</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">node</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_validate</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">_left</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Left child exists&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_size</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="c1"># node is its parent</span>
        <span class="n">node</span><span class="o">.</span><span class="n">_left</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_Node</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">node</span><span class="p">)</span>
        <span class="c1"># [Warning] the line is missed in the book</span>
        <span class="n">node</span><span class="o">.</span><span class="n">_left</span><span class="o">.</span><span class="n">_parent</span> <span class="o">=</span> <span class="n">node</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_position</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">_left</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_add_right</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">e</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a new right child for Position p, storing element e.</span>

<span class="sd">        Return the Position of new node.</span>
<span class="sd">        Raise ValueError if Position p is invalid or</span>
<span class="sd">        p already has a right child.</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">node</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_validate</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">_right</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Right child exists&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_size</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="c1"># node is its parent</span>
        <span class="n">node</span><span class="o">.</span><span class="n">_right</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_Node</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">node</span><span class="p">)</span>
        <span class="c1"># [Warning] the line is missed in the book</span>
        <span class="n">node</span><span class="o">.</span><span class="n">_right</span><span class="o">.</span><span class="n">_parent</span> <span class="o">=</span> <span class="n">node</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_position</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">_right</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_replace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">e</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Replace the element at position p with e, and return old element.</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">node</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_validate</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="n">old</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">_element</span>
        <span class="n">node</span><span class="o">.</span><span class="n">_element</span> <span class="o">=</span> <span class="n">e</span>
        <span class="k">return</span> <span class="n">old</span>

    <span class="k">def</span> <span class="nf">_delete</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Delete the node at Position p,</span>
<span class="sd">        and replace it with its child, if any.</span>

<span class="sd">        Return the element that had been stored at Position p.</span>
<span class="sd">        Raise ValueError if Position p is invalid or p has two children.</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">node</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_validate</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_children</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Position has two children&#39;</span><span class="p">)</span>
        <span class="n">child</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">_left</span> <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">_left</span> <span class="k">else</span> <span class="n">node</span><span class="o">.</span><span class="n">_right</span>  <span class="c1"># might be None</span>
        <span class="k">if</span> <span class="n">child</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">child</span><span class="o">.</span><span class="n">_parent</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">_parent</span>   <span class="c1"># child&#39;s grandparent becomes parent</span>
        <span class="k">if</span> <span class="n">node</span> <span class="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_root</span> <span class="o">=</span> <span class="n">child</span>             <span class="c1"># child becomes root</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">parent</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">_parent</span>
            <span class="k">if</span> <span class="n">node</span> <span class="ow">is</span> <span class="n">parent</span><span class="o">.</span><span class="n">_left</span><span class="p">:</span>
                <span class="n">parent</span><span class="o">.</span><span class="n">_left</span> <span class="o">=</span> <span class="n">child</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">parent</span><span class="o">.</span><span class="n">_right</span> <span class="o">=</span> <span class="n">child</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_size</span> <span class="o">-=</span> <span class="mi">1</span>
        <span class="n">node</span><span class="o">.</span><span class="n">_parent</span> <span class="o">=</span> <span class="n">node</span>              <span class="c1"># convention for deprecated node</span>
        <span class="k">return</span> <span class="n">node</span><span class="o">.</span><span class="n">_element</span>

    <span class="k">def</span> <span class="nf">_attach</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">t1</span><span class="p">,</span> <span class="n">t2</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Attach trees t1 and t2, respectively, as the left and right</span>
<span class="sd">        subtrees of the external Position p.</span>

<span class="sd">        As a side effect, set t1 and t2 to empty.</span>
<span class="sd">        Raise TypeError if trees t1 and t2 do not match type of this tree.</span>
<span class="sd">        Raise ValueError if Position p is invalid or not external.</span>

<span class="sd">        Time complexity: O(1)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">node</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_validate</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_leaf</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;position must be leaf&#39;</span><span class="p">)</span>
        <span class="c1"># all 3 trees must be same type</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">type</span><span class="p">(</span><span class="n">t1</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">type</span><span class="p">(</span><span class="n">t2</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Tree types must match&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_size</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">t1</span><span class="p">)</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">t2</span><span class="p">)</span>
        <span class="c1"># attached t1 as left subtree of node</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">t1</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
            <span class="n">t1</span><span class="o">.</span><span class="n">_root</span><span class="o">.</span><span class="n">_parent</span> <span class="o">=</span> <span class="n">node</span>
            <span class="n">node</span><span class="o">.</span><span class="n">_left</span> <span class="o">=</span> <span class="n">t1</span><span class="o">.</span><span class="n">_root</span>
            <span class="n">t1</span><span class="o">.</span><span class="n">_root</span> <span class="o">=</span> <span class="kc">None</span>             <span class="c1"># set t1 instance to empty</span>
            <span class="n">t1</span><span class="o">.</span><span class="n">_size</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">t2</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>         <span class="c1"># attached t2 as right subtree of node</span>
            <span class="n">t2</span><span class="o">.</span><span class="n">_root</span><span class="o">.</span><span class="n">_parent</span> <span class="o">=</span> <span class="n">node</span>
            <span class="n">node</span><span class="o">.</span><span class="n">_right</span> <span class="o">=</span> <span class="n">t2</span><span class="o">.</span><span class="n">_root</span>
            <span class="n">t2</span><span class="o">.</span><span class="n">_root</span> <span class="o">=</span> <span class="kc">None</span>             <span class="c1"># set t2 instance to empty</span>
            <span class="n">t2</span><span class="o">.</span><span class="n">_size</span> <span class="o">=</span> <span class="mi">0</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Xiangzhuang Shen

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>